package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCLogin2 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()) {
            Scanner scan = new Scanner(System.in);
            System.out.println("用户名：");
            String userName = scan.nextLine();
            System.out.println("密码：");
            String passWord = scan.nextLine();
            //定义预编译SQL语句[?代表占位符]
            String sql = "SELECT nickName FROM userinfo WHERE userName=? AND passWord =?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //设置?的值
            ps.setString(1, userName);
            ps.setString(2, passWord);
            //执行预编译SQL
            ResultSet r = ps.executeQuery();
            if (r.next()) {
                String nickName = r.getString("nickName");
                System.out.println("登录成功！,欢迎您:" + nickName);
            } else {
                System.out.println("用户名或者密码错误");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

}
